﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface;
using BusinessObjects;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer
{
    public class SqlServerRestaurantMenuDao : IRestaurantMenuDao
    {
        //phan nay hien thi list deu make dang no image de nhe nhang khi chuyen du lieu di
        //khi nao hien thi detail moi make ca? image
        public List<RestaurantMenu> GetRestaurantMenusByCategoryId(string menuCategoryId)
        {
            string procedures = "Get_All_RestaurantMenus_ByCondition";

            object[] parms = { "@WhereClause", " WHERE MenuCategoryID = '" + menuCategoryId + "'" };
            return Db.ReadList(procedures, true, MakeNoImage, parms);
        }

        public List<RestaurantMenu> GetRestaurantMenusPagedByBranch(int currentPage, string restaurantId)
        {
            string procedures = "Get_Paged_RestaurantMenus";

            object[] parms = {  "@WhereClause", " RestaurantID = '" + restaurantId + "'", 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, MakeNoImage, parms);
        }

        public List<RestaurantMenu> GetRestaurantMenusActiveByBranch(string restaurantId, bool isActive)
        {
            string procedures = "Get_All_RestaurantMenus_ByCondition";
            int _isActive = 0;
            if (isActive == true) { _isActive = 1; }
            else { _isActive = 0; }

            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId +
                                                                        "' AND IsActive = " + _isActive + 
                                                                        " AND IsNorm = 1" };
            return Db.ReadList(procedures, true, MakeNoImage, parms);
        }

        public List<RestaurantMenu> GetRestaurantMenusPagedByCategory(int currentPage, string menuCategoryId)
        {
            string procedures = "Get_Paged_RestaurantMenus";

            object[] parms = {  "@WhereClause", " MenuCategoryID = '" + menuCategoryId + "'", 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, MakeNoImage, parms);
        }

        public RestaurantMenu GetRestaurantMenu(string menuId)
        {
            string procedures = "Get_All_RestaurantMenus_ByID";

            object[] parms = { "@MenuID", menuId };
            return Db.Read(procedures, true, Make, parms);
        }

        public int GetTotalPageByBranch(string restaurantId)
        {
            string procedures = "Get_PageSize_RestaurantMenu";
            object[] parms = { "@WhereClause", null };

            return Db.ReadTotalPage(procedures, true, parms);
        }

        public int GetTotalPageByCategory(string menuCategoryId)
        {
            string procedures = "Get_PageSize_RestaurantMenu";
            object[] parms = { "@WhereClause", " WHERE MenuCategoryID = '" + menuCategoryId + "'" };

            return Db.ReadTotalPage(procedures, true, parms);
        }

        public void InsertRestaurantMenu(RestaurantMenu restaurantMenu)
        {
            string sql = "Insert_RestaurantMenu";

            restaurantMenu.MenuID = Db.Insert(sql, true, Take(restaurantMenu));
        }

        public void UpdateRestaurantMenu(RestaurantMenu restaurantMenu)
        {
            string sql = "Update_RestaurantMenu";

            Db.Update(sql, true, Take(restaurantMenu));
        }

        public void DeleteRestaurantMenu(RestaurantMenu restaurantMenu)
        {
            string sql = "Delete_RestaurantMenu";

            Db.Update(sql, true, Take(restaurantMenu.MenuID));
        }

        private object[] Take(string menuId)
        {
            return new object[]  
            {
                "@MenuID", menuId,
            };
        }

        private object[] Take(RestaurantMenu restaurantMenu)
        {
            return new object[]  
            {
                "@MenuID", restaurantMenu.MenuID,
                "@MenuCode", restaurantMenu.MenuCode,
                "@MenuName", restaurantMenu.MenuName,
                "@Description", restaurantMenu.Description,
                "@UnitID", restaurantMenu.UnitID,
			    "@SalesPrice", restaurantMenu.SalesPrice,
                "@MenuImage", restaurantMenu.MenuImage ,
                "@IsActive", restaurantMenu.IsActive,
                "@IsNorm", restaurantMenu.IsNorm,
                "@RestaurantID", restaurantMenu.RestaurantID,
                "@MenuCategoryID", restaurantMenu.MenuCategoryID
            };
        }

        private static Func<IDataReader, RestaurantMenu> MakeNoImage = reader =>
            new RestaurantMenu
            {
                MenuID = reader["MenuID"].AsString(),
                MenuCode = reader["MenuCode"].AsString(),
                MenuName = reader["MenuName"].AsString(),
                Description = reader["Description"].AsString(),
                UnitID = reader["UnitID"].AsString(),
                SalesPrice = reader["SalesPrice"].AsDecimal(),
                IsActive = reader["IsActive"].AsBool(),
                IsNorm = reader["IsNorm"].AsBool(),
                RestaurantID = reader["RestaurantID"].AsString(),
                MenuCategoryID = reader["MenuCategoryID"].AsString()
            };

        private static Func<IDataReader, RestaurantMenu> Make = reader =>
            new RestaurantMenu
            {
                MenuID = reader["MenuID"].AsString(),
                MenuCode = reader["MenuCode"].AsString(),
                MenuName = reader["MenuName"].AsString(),
                Description = reader["Description"].AsString(),
                UnitID = reader["UnitID"].AsString(),
                SalesPrice = reader["SalesPrice"].AsDecimal(),
                MenuImage = (byte[])reader["MenuImage"],
                IsActive = reader["IsActive"].AsBool(),
                IsNorm = reader["IsNorm"].AsBool(),
                RestaurantID = reader["RestaurantID"].AsString(),
                MenuCategoryID = reader["MenuCategoryID"].AsString()
            };
    }
}
